package com.scuec.groupfive.mapper;

import com.scuec.groupfive.entity.Questions;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface QuestionMapper_z {

    // 查询题库列表（支持关键词搜索）
    List<Questions> selectQuestions(@Param("keyword") String keyword);

    // 根据ID查询题目
    Questions selectById(@Param("id") Long id);

    // 更新题目基本信息
    @Update("UPDATE questions SET " +
            "subject_id = #{subjectId}, " +
            "question_type = #{questionType}, " +
            "content = #{content}, " +
            "keyword = #{keyword}, " +
            "difficulty = #{difficulty}, " +
            "year = #{year}, " +
            "type_id = #{typeId}, " +
            "chapter_id = #{chapterId}, " +
            "updated_at = NOW() " +
            "WHERE question_id = #{questionId}")
    int updateQuestion(Questions question);

    // 更新选择题答案和解析
    @Update("UPDATE choice_questions SET " +
            "options = #{options}, " +
            "answer = #{answer}, " +
            "analysis = #{analysis}, " +
            "updated_at = NOW() " +
            "WHERE question_id = #{questionId}")
    int updateChoiceQuestion(Questions question);

    // 更新填空题答案和解析
    @Update("UPDATE fill_blank_questions SET " +
            "blanks = #{blanks}, " +
            "answer_template = #{answerTemplate}, " +
            "analysis = #{analysis} " +
            "WHERE question_id = #{questionId}")
    int updateFillBlankQuestion(Questions question);

    // 更新主观题答案和解析
    @Update("UPDATE subjective_questions SET " +
            "reference_answer = #{referenceAnswer}, " +
            "answer_hints = #{answerHints}, " +
            "analysis = #{analysis} " +
            "WHERE question_id = #{questionId}")
    int updateSubjectiveQuestion(Questions question);

    // 插入题目基本信息
    @Insert("INSERT INTO questions " +
            "(subject_id, question_type, content, keyword, difficulty, year, " +
            "creator_id, status, type_id, chapter_id, created_at, updated_at) " +
            "VALUES " +
            "(#{subjectId}, #{questionType}, #{content}, #{keyword}, #{difficulty}, " +
            "#{year}, #{creatorId}, 1, #{typeId}, #{chapterId}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "questionId", keyColumn = "question_id")
    int insertQuestion(Questions question);

    // 插入选择题答案和解析
    @Insert("INSERT INTO choice_questions " +
            "(question_id, options, answer, analysis) " +
            "VALUES " +
            "(#{questionId}, #{options}, #{answer}, #{analysis})")
    int insertChoiceQuestion(Questions question);

    // 插入填空题答案和解析
    @Insert("INSERT INTO fill_blank_questions " +
            "(question_id, blanks, answer_template, analysis) " +
            "VALUES " +
            "(#{questionId}, #{blanks}, #{answerTemplate}, #{analysis})")
    int insertFillBlankQuestion(Questions question);

    // 插入主观题答案和解析
    @Insert("INSERT INTO subjective_questions " +
            "(question_id, reference_answer, answer_hints, analysis) " +
            "VALUES " +
            "(#{questionId}, #{referenceAnswer}, #{answerHints}, #{analysis})")
    int insertSubjectiveQuestion(Questions question);

    // 删除题目相关的答题记录
    @Delete("DELETE FROM answer_records WHERE question_id = #{id}")
    int deleteAnswerRecords(@Param("id") Long id);

    // 删除题目相关的试卷记录
    @Delete("DELETE FROM exam_questions WHERE question_id = #{id}")
    int deleteExamQuestions(@Param("id") Long id);

    // 删除题目（包括关联表数据）
    @Delete("DELETE FROM questions WHERE question_id = #{id}")
    int deleteQuestion(@Param("id") Long id);

    @Delete("DELETE FROM choice_questions WHERE question_id = #{id}")
    int deleteChoiceQuestion(@Param("id") Long id);

    @Delete("DELETE FROM fill_blank_questions WHERE question_id = #{id}")
    int deleteFillBlankQuestion(@Param("id") Long id);

    @Delete("DELETE FROM subjective_questions WHERE question_id = #{id}")
    int deleteSubjectiveQuestion(@Param("id") Long id);

    @Select("SELECT * FROM questions")
    List<Questions> selectAllQuestions();
}